package org.jeecg.modules.job;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.demo.live.entity.JkLive;
import org.jeecg.modules.demo.live.service.IJkLiveService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;

@Slf4j
public class LivaJob implements Job {

    @Autowired
    private IJkLiveService jkLiveService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 计算当前时间前一小时
        LocalDateTime oneHourAgo = LocalDateTime.now().plusHours(5);

        // 构造更新条件
        UpdateWrapper<JkLive> updateWrapper = new UpdateWrapper<>();
        updateWrapper.le("begin_time", oneHourAgo)  // beginTime <= 当前时间前一小时
                .eq("state", "1");        // state = 未开播

        // 设置要更新的字段
        JkLive updateEntity = new JkLive();
        updateEntity.setState("4"); //直播间失效

        // 执行更新（会自动把updateWrapper作为where条件）
        boolean success = jkLiveService.update(updateEntity, updateWrapper);
        log.info("更新直播状态完毕");

    }
}
